var myTableMap = {};
//table.datatables报表注册
$(document).ready(function() {
	$("table.datatables").contextmenu(datatablesConfig);
	$("table.datatables").contextmenu();
});

//执行主函数入口
function datatablesConfig(options){
	
	//table上的属性值封装
	var _defaults = {
        srcEle: this,
        id: $(this).attr('id'),
        scrollY: $(this).attr('datatables-scroll-y'),
        leftColumns: $(this).attr('datatables-left-columns'),
        colvisGroup: $(this).attr('datatables-colvis-group'),
        footerCallback: $(this).attr('datatables-footer-callback')
    };
    var opts = $.extend(true, _defaults, options);
    
    var dataTable = new Object();
    
    //init
    	dataTable.dom='B<"toolbar">frtlp';
    	
    //固定行和列
		dataTable.paging=false;
		dataTable.scrollX=true;
		dataTable.scrollY=opts.scrollY;//datatables-scroll-y
		dataTable.fixedHeader=true;
		dataTable.fixedColumns=true;
		dataTable.fixedColumns={leftColumns:Number(opts.leftColumns)};//datatables-left-columns
	
	//列分组
	var buttons = [];
	eval(opts.colvisGroup);
	dataTable.buttons=buttons;
	dataTable.buttons[dataTable.buttons.length]={extend: 'colvisGroup',text: '全部',show: ':hidden'};
	
	dataTable.footerCallback=function ( row, data, start, end, display ) {
            var api = this.api(), data;
 
            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
			
			if(opts.footerCallback!=undefined){
				$.each(opts.footerCallback.split(","), function(key, val) {
		            // Total over all pages
		            total = api
		                .column( val )
		                .data()
		                .reduce( function (a, b) {
		                    return intVal(a) + intVal(b);
		                } );
		            // Update footer
		            $( api.column( val ).footer() ).html(
		               total
		            );
				});
			}
     };

    if(myTableMap[opts.id]==undefined || myTableMap[opts.id].selector!="#"+opts.id){
    	var myDataTables={};
    	myDataTables = concatJson(myDataTables,datatablesLanguage());
    	myDataTables = concatJson(myDataTables,dataTable);
	    var table = $('#'+opts.id).dataTable(myDataTables);
	    myTableMap[opts.id]=table;
    }
    
    //json复制追加
    function concatJson(o1,o2){
    	for(var i in o2){
    		o1[i] = o2[i];
    	}
    	return o1;
    }
    
    //国际化
    function datatablesLanguage(){
    	return{"language":{
					"sProcessing":   "处理中...",
					"sLengthMenu":   "显示 _MENU_ 项结果",
					"sZeroRecords":  "没有匹配结果",
					"sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
					"sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
					"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
					"sInfoPostFix":  "",
					"sSearch":       "全局搜索:",
					"sUrl":          "",
					"sEmptyTable":     "表中数据为空",
					"sLoadingRecords": "载入中...",
					"sInfoThousands":  ",",
					"oPaginate": {
						"sFirst":    "首页",
						"sPrevious": "上页",
						"sNext":     "下页",
						"sLast":     "末页"
					},
					"oAria": {
						"sSortAscending":  ": 以升序排列此列",
						"sSortDescending": ": 以降序排列此列"
					},
					"buttons": {
						"colvis": "显示隐藏列",
						"colvisRestore": "重置列"
					}}}
    }
}
